from flask import Blueprint, request, jsonify
from flask_jwt_extended import jwt_required, get_jwt_identity
from models import User, db
from utils.auth import bcrypt

settings_bp = Blueprint('settings', __name__)

@settings_bp.route('/api/settings/password', methods=['PUT'])
@jwt_required()
def change_password():
    user_id = get_jwt_identity()
    data = request.json
    current_password = data.get('current_password')
    new_password = data.get('new_password')
    
    user = User.query.get(user_id)
    if not bcrypt.check_password_hash(user.password_hash, current_password):
        return jsonify(code=400, msg='当前密码错误'), 400
    
    user.password_hash = bcrypt.generate_password_hash(new_password).decode('utf-8')
    db.session.commit()
    return jsonify(code=200, msg='密码修改成功')

@settings_bp.route('/api/settings/notification', methods=['PUT'])
@jwt_required()
def update_notification():
    user_id = get_jwt_identity()
    data = request.json
    # 假设用户模型新增通知设置字段
    user = User.query.get(user_id)
    user.notification_enabled = data.get('enabled', False)
    db.session.commit()
    return jsonify(code=200, msg='通知设置已更新')